var app = getApp();
Page({
    data:{
        currType:0,
        catelist:[],
        goodsInfo:[],
        host:app.d.hostUrl,

    },

    //预加载初始化数据
    onLoad:function(options){
        var that = this;
        wx.request({
            url: app.d.hostUrl + '/api/cate-list',
            method : 'get',
            data : {},
            header: {
                'Content-Type':  'application/x-www-form-urlencoded'
            },

            success: function(res){
                var status = res.data.status;
                var data = res.data.info;
                if(status == 0){
                    wx.showToast({
                        title: data,
                        duration: 2000
                    });
                    return false;
                };
                that.setData({
                    catelist : data.cateInfo,
                    goodsInfo : data.goodsInfo,
                    currType : data.cateInfo[0].id,
                });
            },
            error:function(e){
                wx.showToast({
                    title: '网络异常！',
                    duration: 2000
                });
                return false;
            },
        });
    },

    //根据分类请求不同的商品
    tapType:function(res){
        var that = this;
        const currType = res.currentTarget.dataset.typeId;
        that.setData({
            currType: currType
        });
        wx.request({
            url: app.d.hostUrl + '/api/goods-list',
            method: 'get',
            data:{
                cate_id:currType
            },
            header: {
                'Content-Type':  'application/x-www-form-urlencoded'
            },
            success:function(res){
                var status = res.data.status;
                var data = res.data.info;
                if(status == 0){
                    wx.showToast({
                        title: data,
                        duration: 2000
                    });
                    return false;
                }
                that.setData({
                    goodsInfo : data,
                });
            },
            error:function(e){
                wx.showToast({
                    title: '网络异常！',
                    duration: 2000
                });
                return false;
            },
        });
    },

    //加入购物车
  addCart:function(res){
    var that = this;
    var goodsId = res.currentTarget.dataset.goodsid;
    wx.request({
        url: app.d.hostUrl + '/api/add-cart-index',
        method: 'get',
        data: {
            user_id: app.d.userId,
            goods_id: goodsId
        },
        header: {
            'Content-Type':  'application/x-www-form-urlencoded'
        },

        success:function(res){
            var status = res.data.status;
            var data = res.data.info;
            if(status == 0){
                wx.showToast({
                    title: data,
                    duration: 2000
                });
                return false;
            }else{
                wx.showToast({
                    title: data,
                    duration: 2000
                });
            }
        },
        error:function(e){
            wx.showToast({
                title: '网络错误！',
                duration: 2000
            });
            return false;
        },

    });
  },
});